var app = getApp()
var util = require("../../utils/util.js")
var userModel = require("../../model/userModel.js")
var userUtil = require('../../utils/userUtil.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentCellphone: null, //发送验证码时的手机号
    cellphone: null, //当前输入框的手机号
    cd: 0, //发送验证码间隔时间
    inputCode: "", //当前验证码
    code: null, //服务端验证码
    display: 'none',
    isLogin: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var isLogin = userUtil.isLogin()
    if (!isLogin) {
      this.setData({
        display: 'flex'
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  //监听手机号输入框
  inputCellphone(e) {
    var val = e.detail.value
    var inputCode = this.data.inputCode
    this.setData({
      cellphone: val,
      isLogin: this.getIsLogin(val, inputCode)
    })
  },
  //监听验证码输入框
  inputCode(e) {
    var val = e.detail.value
    var cellphone = this.data.cellphone
    this.setData({
      inputCode: val,
      isLogin: this.getIsLogin(cellphone, val)
    })
  },
  getIsLogin(cellphone, code) {
    var isLogin = true;
    if (!/^[1][3,4,5,7,8][0-9]{9}$/.test(cellphone)) isLogin = false;
    if (!code) isLogin = false;
    console.log(isLogin)
    return isLogin;
  },
  //获取用户公开信息
  bindgetuserinfo(e) {
    var user = e.detail.userInfo
    if (!user) wx.navigateBack({})
    this.setData({
      display: 'none',
      user: user
    })
  },

  //发送验证码
  bindSendCode() {
    var that = this
    var cellphone = this.data.cellphone
    if (!/^[1][3,4,5,7,8][0-9]{9}$/.test(cellphone)) {
      util.failToast("请输入有效手机号")
      return
    }
    this.setData({
      cd: 5
    })
    //计时
    var timer = setInterval(() => {
      var cd = that.data.cd - 1
      if (cd == 0) clearInterval(timer)
      that.setData({
        cd: cd
      })
    }, 1000)
    this.sendCode(cellphone)
  },
  //发送验证码
  sendCode(cellphone) {
    this.setData({
      currentCellphone: cellphone
    })
    var that = this
    userModel.sendCode(cellphone, res => {
      if (res.status == 1) {
        util.failToast(res.msg)
        return
      }
      // util.successToast(res.data)
      that.setData({
        code: res.data.code,
        inputCode: res.data.code,
        isLogin: true
      })
    })
  },
  //登录
  bindLogin() {
    //验证输入框手机号和发送验证码时手机号是否 一致
    var cellphone = this.data.cellphone
    var currentCellphone = this.data.currentCellphone
    if (cellphone != currentCellphone) {
      util.failToast("请重新发送验证码")
      return
    }
    if (!this.data.inputCode) {
      util.failToast("请输入验证码")
      return
    }
    //验证验证码和服务端验证码是否一致
    if (this.data.code != this.data.inputCode) {
      util.failToast("验证码错误")
      return
    }
    util.showLoading('请稍后')
    var user = this.data.user
    var data = {
      nickname: user.nickName,
      cellphone: cellphone,
      gender: user.gender,
      avatar: user.avatarUrl
    }
    //提交绑定信息
    userModel.login(data, res => {
      if (res.status == 1) {
        util.failToast(res.msg)
        return
      }
      app.data.user = res.data
      wx.setStorageSync("user", res.data)
      wx.navigateBack({
        delta: 1
      })
    })
  }
})